﻿using Common;
using DevExpress.XtraEditors;
using DLL;
using Newtonsoft.Json;
using NursingFormSystem.Common;
using NursingFormSystem.Model;
using SQLDLL;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NursingFormSystem.SpecialPopulationAssessmentFormNew
{
    public partial class HistorySPAF : Form
    {
        #region 变量声明
        NurFormsService.NursingForm formService = new NurFormsService.NursingForm();
        TriageInfoSystem.WebService1 TriageInfoSystem = new TriageInfoSystem.WebService1();
        NursingFormRecord record = new NursingFormRecord();
        Tb_User LoginUser; //当前的登录信息
        DataTable NewPatientInfo;  //患者信息 用于接收list中患者
        PatientInfo PatientInfo;
        string PatientId;
        string Id;
        string PatientName;  //患者姓名
        #endregion

        #region 构造函数（文书、急诊）
        public HistorySPAF(Tb_User loginUser, PatientInfo patientInfo)
        {
            InitializeComponent();
            this.LoginUser = loginUser;
            this.PatientInfo = patientInfo;
            this.dateE_StartTime.Text = DateTime.Now.ToString("yyyy-MM-dd");
            InitData(this.dateE_StartTime.Text);
        }
        public HistorySPAF(string[] loginUser,string[] patient)
        {
            InitializeComponent();
            Tb_User loginU = new Tb_User();
            loginU.UserId = loginUser[0];
            loginU.UserName = loginUser[1];
            loginU.DepartmentCode = loginUser[2];
            loginU.DepartmentName = loginUser[3];
            loginU.JobTitle = loginUser[4];
            loginU.PositionTitle = loginUser[5];
            this.LoginUser = loginU;
            this.dateE_StartTime.Text = DateTime.Now.ToString("yyyy-MM-dd");
            //获取当急诊科室的患者列表
            InitData(this.dateE_StartTime.Text);
        }
        #endregion

        #region  初始化患者信息
        /// <summary>
        /// 初始化相关在院 患者信息下拉框按钮；
        /// </summary>
        private void InitData(string dateTime)
        {
            listBoxControl1.Items.Clear();
            string startDate = this.dateE_StartTime.DateTime.ToString("yyyy-MM-dd 00:00:00");
            string endDate = this.dateE_StartTime.DateTime.AddDays(1).ToString("yyyy-MM-dd 00:00:00");
            TriageInfoSystem.ResultHelperOfPage res = TriageInfoSystem.getTb_TriageInfo(-1,"", "", startDate, endDate, "", "", "", "", "", "", -1, 1, 100000);
            if (res.S)
            {
                TriageInfoSystem.Page page = res.D;
                DataTable dt = page.DT;
                NewPatientInfo = dt;
                if (dt != null && dt.Rows.Count > 0)
                {
                    foreach (DataRow row in dt.Rows)
                    {
                        listBoxControl1.Items.Add(row["Name"]);
                    }
                    listBoxControl1.SelectedIndex = 0;
                }
            }
        }
        #endregion


        #region 患者列表选择改变事件
        private void listBoxControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBoxControl1.ItemCount == 0) return;
            if (listBoxControl1.SelectedIndex != -1)
            {
                int index = listBoxControl1.SelectedIndex;
                this.PatientId = NewPatientInfo.Rows[index]["TriageId"].ToString();  //患者ID
                this.PatientName = NewPatientInfo.Rows[index]["Name"].ToString();  //姓名
                if (string.IsNullOrEmpty(PatientId)) return;
                BindInfo(this.PatientId);
            }
        }
        #endregion

        #region 时间改变事件
        private void dateE_StartTime_TextChanged(object sender, EventArgs e)
        {
            InitData(this.dateE_StartTime.Text);
        }
        #endregion

        #region 绑定数据
        private void BindInfo(string id)
        {
            textqianming.Text = this.LoginUser.UserName;
            if (string.IsNullOrEmpty(id)) { InsertMode(false); return; }
            string sql = string.Format("SELECT NursingFormData FROM NursingFormRecord(NOLOCK) WHERE PatientId='{0}' ", id);
            object data = SqlHelper.ExecuteScalar(CommandType.Text, sql);
            if (data == DBNull.Value) { InsertMode(false); return; }
            if (data == null) { InsertMode(false); return; }
            if (string.IsNullOrEmpty(data.ToString())) { InsertMode(false); return; }
            Spaf spaf = new Spaf();
            spaf = XmlUtil.XMLToObject<Spaf>(data.ToString(), Encoding.UTF8);

            chkChangqitengtongrq0.Checked = spaf.ChangQiTengTongHuanZhe.ChangQiTengTongRenQun.Split('#')[0].Equals("1");
            chkChangqitengtongrq1.Checked = spaf.ChangQiTengTongHuanZhe.ChangQiTengTongRenQun.Split('#')[1].Equals("1");

            checkchulicuoshichangqi1.Checked = spaf.ChangQiTengTongHuanZhe.ChuLiCuoShi_ChangQi.Split('#')[0].Equals("1");
            checkchulicuoshichangqi2.Checked = spaf.ChangQiTengTongHuanZhe.ChuLiCuoShi_ChangQi.Split('#')[1].Equals("1");
            checkchulicuoshichangqi3.Checked = spaf.ChangQiTengTongHuanZhe.ChuLiCuoShi_ChangQi.Split('#')[2].Equals("1");
            checkchulicuoshichangqi4.Checked = spaf.ChangQiTengTongHuanZhe.ChuLiCuoShi_ChangQi.Split('#')[3].Equals("1");

            checkduihuanzheyingxiang1.Checked = spaf.ChangQiTengTongHuanZhe.DuiHuanZheYingXiang.Split('#')[0].Equals("1");
            checkduihuanzheyingxiang2.Checked = spaf.ChangQiTengTongHuanZhe.DuiHuanZheYingXiang.Split('#')[1].Equals("1");
            checkduihuanzheyingxiang3.Checked = spaf.ChangQiTengTongHuanZhe.DuiHuanZheYingXiang.Split('#')[2].Equals("1");
            checkduihuanzheyingxiang4.Checked = spaf.ChangQiTengTongHuanZhe.DuiHuanZheYingXiang.Split('#')[3].Equals("1");

            checkhuanzheduizhitongyao1.Checked = spaf.ChangQiTengTongHuanZhe.HuanZheDuiZhiTongYaoChengYinKenNegXing.Split('#')[0].Equals("1");
            checkhuanzheduizhitongyao2.Checked = spaf.ChangQiTengTongHuanZhe.HuanZheDuiZhiTongYaoChengYinKenNegXing.Split('#')[1].Equals("1");
            checkhuanzheduizhitongyao3.Checked = spaf.ChangQiTengTongHuanZhe.HuanZheDuiZhiTongYaoChengYinKenNegXing.Split('#')[2].Equals("1");

            checkyizhounei1.Checked = spaf.ChangQiTengTongHuanZhe.YiZhouNeiCenYongYao.Split('#')[0].Equals("1");
            checkyizhounei2.Checked = spaf.ChangQiTengTongHuanZhe.YiZhouNeiCenYongYao.Split('#')[1].Equals("1");
            checkyizhounei3.Checked = spaf.ChangQiTengTongHuanZhe.YiZhouNeiCenYongYao.Split('#')[2].Equals("1");
            checkyizhounei4.Checked = spaf.ChangQiTengTongHuanZhe.YiZhouNeiCenYongYao.Split('#')[3].Equals("1");
            checkyizhounei5.Checked = spaf.ChangQiTengTongHuanZhe.YiZhouNeiCenYongYao.Split('#')[4].Equals("1");

            checkyongyaoxiaoguo1.Checked = spaf.ChangQiTengTongHuanZhe.YongYaoHouXiaoGuo.Split('#')[0].Equals("1");
            checkyongyaoxiaoguo2.Checked = spaf.ChangQiTengTongHuanZhe.YongYaoHouXiaoGuo.Split('#')[1].Equals("1");
            checkyongyaoxiaoguo3.Checked = spaf.ChangQiTengTongHuanZhe.YongYaoHouXiaoGuo.Split('#')[2].Equals("1");

            checkyuanyin1.Checked = spaf.ChangQiTengTongHuanZhe.YuanYin.Split('#')[0].Equals("1");
            checkyuanyin2.Checked = spaf.ChangQiTengTongHuanZhe.YuanYin.Split('#')[1].Equals("1");
            checkyuanyin3.Checked = spaf.ChangQiTengTongHuanZhe.YuanYin.Split('#')[2].Equals("1");
            checkyuanyin4.Checked = spaf.ChangQiTengTongHuanZhe.YuanYin.Split('#')[3].Equals("1");
            checkyuanyin5.Checked = spaf.ChangQiTengTongHuanZhe.YuanYin.Split('#')[4].Equals("1");
            checkyuanyin6.Checked = spaf.ChangQiTengTongHuanZhe.YuanYin.Split('#')[5].Equals("1");
            checkyuanyin7.Checked = spaf.ChangQiTengTongHuanZhe.YuanYin.Split('#')[6].Equals("1");
            checkyuanyin8.Checked = spaf.ChangQiTengTongHuanZhe.YuanYin.Split('#')[7].Equals("1");

            checkchulicuoshizisha1.Checked = spaf.ZiShaQingXiangHuanZhe.ChuLioCuoShi_ZiSha.Split('#')[0].Equals("1");
            checkchulicuoshizisha2.Checked = spaf.ZiShaQingXiangHuanZhe.ChuLioCuoShi_ZiSha.Split('#')[1].Equals("1");
            checkchulicuoshizisha3.Checked = spaf.ZiShaQingXiangHuanZhe.ChuLioCuoShi_ZiSha.Split('#')[2].Equals("1");
            checkchulicuoshizisha4.Checked = spaf.ZiShaQingXiangHuanZhe.ChuLioCuoShi_ZiSha.Split('#')[3].Equals("1");
            checkchulicuoshizisha5.Checked = spaf.ZiShaQingXiangHuanZhe.ChuLioCuoShi_ZiSha.Split('#')[4].Equals("1");

            checkweixdianyinsu1.Checked = spaf.ZiShaQingXiangHuanZhe.WeiXianYinSu.Split('#')[0].Equals("1");
            checkweixdianyinsu2.Checked = spaf.ZiShaQingXiangHuanZhe.WeiXianYinSu.Split('#')[1].Equals("1");
            checkweixdianyinsu3.Checked = spaf.ZiShaQingXiangHuanZhe.WeiXianYinSu.Split('#')[2].Equals("1");
            checkweixdianyinsu4.Checked = spaf.ZiShaQingXiangHuanZhe.WeiXianYinSu.Split('#')[3].Equals("1");
            checkweixdianyinsu5.Checked = spaf.ZiShaQingXiangHuanZhe.WeiXianYinSu.Split('#')[4].Equals("1");
            checkweixdianyinsu6.Checked = spaf.ZiShaQingXiangHuanZhe.WeiXianYinSu.Split('#')[5].Equals("1");

            checkzisha1.Checked = spaf.ZiShaQingXiangHuanZhe.ZiShaQingXiang.Split('#')[0].Equals("1");
            checkzisha2.Checked = spaf.ZiShaQingXiangHuanZhe.ZiShaQingXiang.Split('#')[1].Equals("1");

            checkchulicuoshilinzhong1.Checked = spaf.LinZhongHuanZhe.ChuLioCuo_LinZhong.Split('#')[0].Equals("1");
            checkchulicuoshilinzhong2.Checked = spaf.LinZhongHuanZhe.ChuLioCuo_LinZhong.Split('#')[1].Equals("1");
            checkchulicuoshilinzhong3.Checked = spaf.LinZhongHuanZhe.ChuLioCuo_LinZhong.Split('#')[2].Equals("1");
            checkchulicuoshilinzhong4.Checked = spaf.LinZhongHuanZhe.ChuLioCuo_LinZhong.Split('#')[3].Equals("1");

            checkqiangjiu1.Checked = spaf.LinZhongHuanZhe.HuanZheJiShuLinZhongQiangJiuTaiDu.Split('#')[0].Equals("1");
            checkqiangjiu2.Checked = spaf.LinZhongHuanZhe.HuanZheJiShuLinZhongQiangJiuTaiDu.Split('#')[1].Equals("1");
            checkqiangjiu3.Checked = spaf.LinZhongHuanZhe.HuanZheJiShuLinZhongQiangJiuTaiDu.Split('#')[2].Equals("1");

            checkhuanzheqingxu1.Checked = spaf.LinZhongHuanZhe.HuanZheQingXu.Split('#')[0].Equals("1");
            checkhuanzheqingxu2.Checked = spaf.LinZhongHuanZhe.HuanZheQingXu.Split('#')[1].Equals("1");
            checkhuanzheqingxu3.Checked = spaf.LinZhongHuanZhe.HuanZheQingXu.Split('#')[2].Equals("1");
            checkhuanzheqingxu4.Checked = spaf.LinZhongHuanZhe.HuanZheQingXu.Split('#')[3].Equals("1");
            checkhuanzheqingxu5.Checked = spaf.LinZhongHuanZhe.HuanZheQingXu.Split('#')[4].Equals("1");

            checkhuanzhexuqiu1.Checked = spaf.LinZhongHuanZhe.HuanZheXuQiu.Split('#')[0].Equals("1");
            checkhuanzhexuqiu2.Checked = spaf.LinZhongHuanZhe.HuanZheXuQiu.Split('#')[1].Equals("1");
            checkhuanzhexuqiu3.Checked = spaf.LinZhongHuanZhe.HuanZheXuQiu.Split('#')[2].Equals("1");

            checkjiarenxuqiu.Checked = spaf.LinZhongHuanZhe.JiaRenXuQiu.Equals("1");

            textjibingzhenduan.Text = spaf.LinZhongHuanZhe.JiBingZhenDuan;

            checklinzhonghuanzhe1.Checked = spaf.LinZhongHuanZhe.LZHZ.Split('#')[0].Equals("1");
            checklinzhonghuanzhe2.Checked = spaf.LinZhongHuanZhe.LZHZ.Split('#')[1].Equals("1");

            checkzhuyaozhengzhuang1.Checked = spaf.LinZhongHuanZhe.ZhuYaoZhengZhuang.Split('#')[0].Equals("1");
            checkzhuyaozhengzhuang2.Checked = spaf.LinZhongHuanZhe.ZhuYaoZhengZhuang.Split('#')[1].Equals("1");
            checkzhuyaozhengzhuang3.Checked = spaf.LinZhongHuanZhe.ZhuYaoZhengZhuang.Split('#')[2].Equals("1");
            checkzhuyaozhengzhuang4.Checked = spaf.LinZhongHuanZhe.ZhuYaoZhengZhuang.Split('#')[3].Equals("1");
            checkzhuyaozhengzhuang5.Checked = spaf.LinZhongHuanZhe.ZhuYaoZhengZhuang.Split('#')[4].Equals("1");
            checkzhuyaozhengzhuang6.Checked = spaf.LinZhongHuanZhe.ZhuYaoZhengZhuang.Split('#')[5].Equals("1");
            checkzhuyaozhengzhuang7.Checked = spaf.LinZhongHuanZhe.ZhuYaoZhengZhuang.Split('#')[6].Equals("1");
            checkzhuyaozhengzhuang8.Checked = spaf.LinZhongHuanZhe.ZhuYaoZhengZhuang.Split('#')[7].Equals("1");

            checkchulicuoshijingshen1.Checked = spaf.JinShenZhangAiHuanZhe.ChuLioCuo_JingShen.Split('#')[0].Equals("1");
            checkchulicuoshijingshen2.Checked = spaf.JinShenZhangAiHuanZhe.ChuLioCuo_JingShen.Split('#')[1].Equals("1");
            checkchulicuoshijingshen3.Checked = spaf.JinShenZhangAiHuanZhe.ChuLioCuo_JingShen.Split('#')[2].Equals("1");
            checkchulicuoshijingshen4.Checked = spaf.JinShenZhangAiHuanZhe.ChuLioCuo_JingShen.Split('#')[3].Equals("1");

            checkfuzaxiangxiang1.Checked = spaf.JinShenZhangAiHuanZhe.FuFaXianXiang.Split('#')[0].Equals("1");
            checkfuzaxiangxiang2.Checked = spaf.JinShenZhangAiHuanZhe.FuFaXianXiang.Split('#')[1].Equals("1");

            checkjiazushi1.Checked = spaf.JinShenZhangAiHuanZhe.JiaZuShi.Split('#')[0].Equals("1");
            checkjiazushi2.Checked = spaf.JinShenZhangAiHuanZhe.JiaZuShi.Split('#')[1].Equals("1");

            checkjingshenzhangai1.Checked = spaf.JinShenZhangAiHuanZhe.JingShenZhangAi.Split('#')[0].Equals("1");
            checkjingshenzhangai2.Checked = spaf.JinShenZhangAiHuanZhe.JingShenZhangAi.Split('#')[1].Equals("1");

            timemocifabingshijian.EditValue = spaf.JinShenZhangAiHuanZhe.MoCiFaBingShiJian;

            checkpeihezhiliao1.Checked = spaf.JinShenZhangAiHuanZhe.PiHeZhiLiao.Split('#')[0].Equals("1");
            checkpeihezhiliao2.Checked = spaf.JinShenZhangAiHuanZhe.PiHeZhiLiao.Split('#')[1].Equals("1");

            timeshoucifabingshijian.Text = spaf.JinShenZhangAiHuanZhe.ShouCiFaBingShiJian;

            checkxinlizhuangkuang1.Checked = spaf.JinShenZhangAiHuanZhe.XinLiZhuangKuang.Split('#')[0].Equals("1");
            checkxinlizhuangkuang2.Checked = spaf.JinShenZhangAiHuanZhe.XinLiZhuangKuang.Split('#')[1].Equals("1");
            checkxinlizhuangkuang3.Checked = spaf.JinShenZhangAiHuanZhe.XinLiZhuangKuang.Split('#')[2].Equals("1");
            checkxinlizhuangkuang4.Checked = spaf.JinShenZhangAiHuanZhe.XinLiZhuangKuang.Split('#')[3].Equals("1");

            checkzhengguizhiliao1.Checked = spaf.JinShenZhangAiHuanZhe.ZhengGuiZhiLiao.Split('#')[0].Equals("1");
            checkzhengguizhiliao2.Checked = spaf.JinShenZhangAiHuanZhe.ZhengGuiZhiLiao.Split('#')[1].Equals("1");

            textxiaoguo.Text = spaf.JinShenZhangAiHuanZhe.XiaoGuo;

            checkchulicuoshixiujiu1.Checked = spaf.XuJiuHuanZhe.ChuLioCuo_XuJiu.Split('#')[0].Equals("1");
            checkchulicuoshixiujiu2.Checked = spaf.XuJiuHuanZhe.ChuLioCuo_XuJiu.Split('#')[1].Equals("1");
            checkchulicuoshixiujiu3.Checked = spaf.XuJiuHuanZhe.ChuLioCuo_XuJiu.Split('#')[2].Equals("1");

            checkxinliyali1.Checked = spaf.XuJiuHuanZhe.XinLiYaLi_XuJiu.Split('#')[0].Equals("1");
            checkxinliyali2.Checked = spaf.XuJiuHuanZhe.XinLiYaLi_XuJiu.Split('#')[1].Equals("1");
            checkxinliyali3.Checked = spaf.XuJiuHuanZhe.XinLiYaLi_XuJiu.Split('#')[2].Equals("1");
            checkxinliyali4.Checked = spaf.XuJiuHuanZhe.XinLiYaLi_XuJiu.Split('#')[3].Equals("1");

            checkxujiu1.Checked = spaf.XuJiuHuanZhe.XuJiu.Split('#')[0].Equals("1");
            checkxujiu2.Checked = spaf.XuJiuHuanZhe.XuJiu.Split('#')[1].Equals("1");

            checkxujiuqingkuang1.Checked = spaf.XuJiuHuanZhe.XuJiuQingKuang.Split('#')[0].Equals("1");
            checkxujiuqingkuang2.Checked = spaf.XuJiuHuanZhe.XuJiuQingKuang.Split('#')[1].Equals("1");
            checkxujiuqingkuang3.Checked = spaf.XuJiuHuanZhe.XuJiuQingKuang.Split('#')[2].Equals("1");
            checkxujiuqingkuang4.Checked = spaf.XuJiuHuanZhe.XuJiuQingKuang.Split('#')[3].Equals("1");

            checkxujiuyuanyin1.Checked = spaf.XuJiuHuanZhe.XuJiuYunaYin.Split('#')[0].Equals("1");
            checkxujiuyuanyin2.Checked = spaf.XuJiuHuanZhe.XuJiuYunaYin.Split('#')[1].Equals("1");
            checkxujiuyuanyin3.Checked = spaf.XuJiuHuanZhe.XuJiuYunaYin.Split('#')[2].Equals("1");

            checkjiujingzhongdu1.Checked = spaf.XuJiuHuanZhe.XuJiuZhongDuXingJiBing.Split('#')[0].Equals("1");
            checkjiujingzhongdu2.Checked = spaf.XuJiuHuanZhe.XuJiuZhongDuXingJiBing.Split('#')[1].Equals("1");

            //吸毒
            checkchulicuoshixidu1.Checked = spaf.XiDuHuanZhe.ChuLioCuo_XiDu.Split('#')[0].Equals("1");
            checkchulicuoshixidu2.Checked = spaf.XiDuHuanZhe.ChuLioCuo_XiDu.Split('#')[1].Equals("1");
            checkchulicuoshixidu3.Checked = spaf.XiDuHuanZhe.ChuLioCuo_XiDu.Split('#')[2].Equals("1");

            checkfuxixianxiang1.Checked = spaf.XiDuHuanZhe.FuXiXianXiang.Split('#')[0].Equals("1");
            checkfuxixianxiang2.Checked = spaf.XiDuHuanZhe.FuXiXianXiang.Split('#')[1].Equals("1");
            checkfuxixianxiang3.Checked = spaf.XiDuHuanZhe.FuXiXianXiang.Split('#')[2].Equals("1");

            checkhbsag1.Checked = spaf.XiDuHuanZhe.HBsAg.Split('#')[0].Equals("1");
            checkhbsag2.Checked = spaf.XiDuHuanZhe.HBsAg.Split('#')[1].Equals("1");
            checkhbsag3.Checked = spaf.XiDuHuanZhe.HBsAg.Split('#')[2].Equals("1");

            checkkanghiv1.Checked = spaf.XiDuHuanZhe.KangHIV.Split('#')[0].Equals("1");
            checkkanghiv2.Checked = spaf.XiDuHuanZhe.KangHIV.Split('#')[1].Equals("1");
            checkkanghiv3.Checked = spaf.XiDuHuanZhe.KangHIV.Split('#')[2].Equals("1");

            checkxiduhuanzhe1.Checked = spaf.XiDuHuanZhe.XDHZ.Split('#')[0].Equals("1");
            checkxiduhuanzhe2.Checked = spaf.XiDuHuanZhe.XDHZ.Split('#')[1].Equals("1");

            checkxidufangshi1.Checked = spaf.XiDuHuanZhe.XiDuFangShi.Split('#')[0].Equals("1");
            checkxidufangshi2.Checked = spaf.XiDuHuanZhe.XiDuFangShi.Split('#')[1].Equals("1");
            checkxidufangshi3.Checked = spaf.XiDuHuanZhe.XiDuFangShi.Split('#')[2].Equals("1");

            checkxidupinlv1.Checked = spaf.XiDuHuanZhe.XiDuPinLv.Split('#')[0].Equals("1");
            checkxidupinlv2.Checked = spaf.XiDuHuanZhe.XiDuPinLv.Split('#')[1].Equals("1");

            checkxinliyalixidu1.Checked = spaf.XiDuHuanZhe.XinLiYaLi_XiDu.Split('#')[0].Equals("1");
            checkxinliyalixidu2.Checked = spaf.XiDuHuanZhe.XinLiYaLi_XiDu.Split('#')[1].Equals("1");
            checkxinliyalixidu3.Checked = spaf.XiDuHuanZhe.XinLiYaLi_XiDu.Split('#')[2].Equals("1");
            checkxinliyalixidu4.Checked = spaf.XiDuHuanZhe.XinLiYaLi_XiDu.Split('#')[3].Equals("1");

            checkzhengguijiedu1.Checked = spaf.XiDuHuanZhe.ZhengGuiJieDuZhiLiao.Split('#')[0].Equals("1");
            checkzhengguijiedu2.Checked = spaf.XiDuHuanZhe.ZhengGuiJieDuZhiLiao.Split('#')[1].Equals("1");

            //签名
            textqianming.Text = spaf.HuShiQianMing;
            dateEditriqi.EditValue = Convert.ToDateTime(spaf.RiQi).ToString("yyyy-MM-dd");

        }
        #endregion


        #region 删除
        private void btnDel_Click_1(object sender, EventArgs e)
        {
            string sqlStr = " SELECT TOP 1 Id FROM dbo.NursingFormRecord(NOLOCK) WHERE PatientId='{0}' AND FormId='{1}' ORDER BY  AssessmentDate DESC ";
            sqlStr = string.Format(sqlStr, PatientId, (int)FormType.NewSpaForm);
            object id = SqlHelper.ExecuteScalar(sqlStr);
            if (id == DBNull.Value) return;
            if (id == null) return;
            if (string.IsNullOrEmpty(id.ToString())) return;
            this.Id = id.ToString();
            if (string.IsNullOrEmpty(id.ToString())) { MessageBox.Show("当前为新增模式，无法操作"); return; }
            if (MessageBox.Show("确定要删除数据吗？", "删除前确认", MessageBoxButtons.OKCancel) != DialogResult.OK) return;

            var ret = formService.DeleteNursingFormRecord(this.Id);
            MessageBox.Show(ret.M);
            if (ret.Code != 1) return;

            string sql = " SELECT TOP 1 Id FROM dbo.NursingFormRecord(NOLOCK) WHERE PatientId='{0}' AND FormId='{1}' ORDER BY  AssessmentDate DESC ";
            sql = string.Format(sql, PatientId, (int)FormType.NewSpaForm);
            object id1 = SqlHelper.ExecuteScalar(CommandType.Text, sql);
            if (id1 == DBNull.Value) { ShowClose(); return; }
            if (id1 == null) { ShowClose(); return; }
            if (string.IsNullOrEmpty(id.ToString())) { ShowClose(); return; }
            this.Id = id1.ToString();
            BindInfo(this.Id);
        }
        /// <summary>
        /// 没有记录单的时候
        /// </summary>
        private void ShowClose()
        {
            MessageBox.Show(string.Format("该患者没有《{0}》 \r\n 自动转入新增界面", EnumHelper.GetDescription(FormType.NewSpaForm)),
                "关闭提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            InsertMode(false);
        }

        private void InsertMode(bool openTips)
        {
            if (openTips && MessageBox.Show("是否新增该表单", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) != DialogResult.OK)
                return;
            foreach (Control c in groupControl1.Controls)
                ControlValueClear(c);
            foreach (Control c in groupControl2.Controls)
                ControlValueClear(c);
            foreach (Control c in groupControl3.Controls)
                ControlValueClear(c);
            foreach (Control c in groupControl4.Controls)
                ControlValueClear(c);
            foreach (Control c in groupControl5.Controls)
                ControlValueClear(c);
            foreach (Control c in groupControl6.Controls)
                ControlValueClear(c);
            this.Id = string.Empty;
            record = new NursingFormRecord();
            dateEditriqi.DateTime = DateTime.Now;
        }

        /// <summary>
        /// 清除控件值
        /// </summary>
        /// <param name="c"></param>
        private static void ControlValueClear(Control c)
        {
            if (c is DateEdit)
                ((DateEdit)c).Text = string.Empty;
            if (c is TextEdit)
                ((TextEdit)c).Text = string.Empty;
            if (c is CheckEdit)
                ((CheckEdit)c).Checked = false;
            if (c is RadioGroup)
                ((RadioGroup)c).SelectedIndex = -1;
            if (c is ComboBoxEdit)
                ((ComboBoxEdit)c).SelectedIndex = -1;
        }
        #endregion


        #region 刷新
        private void btnSearch_Click(object sender, EventArgs e)
        {
            string sql = " SELECT TOP 1 Id FROM dbo.NursingFormRecord(NOLOCK) WHERE PatientId='{0}' AND FormId='{1}' ORDER BY  AssessmentDate DESC ";
            sql = string.Format(sql, PatientId, (int)FormType.NewSpaForm);
            object id = SqlHelper.ExecuteScalar(sql);
            if (id == DBNull.Value) return;
            if (id == null) return;
            if (string.IsNullOrEmpty(id.ToString())) return;
            this.Id = id.ToString();
            BindInfo(this.Id);
        }
        #endregion

        #region  保存
        private void btn_Preservation_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(dateEditriqi.Text))
                dateEditriqi.DateTime = DateTime.Now;
            Spaf spaf = new Spaf();
            spaf.ChangQiTengTongHuanZhe.ChangQiTengTongRenQun =
                (chkChangqitengtongrq0.Checked ? 1 : 0) + "#" +
                (chkChangqitengtongrq1.Checked ? 1 : 0);

            spaf.ChangQiTengTongHuanZhe.ChuLiCuoShi_ChangQi =
                (checkchulicuoshichangqi1.Checked ? 1 : 0) + "#" +
                (checkchulicuoshichangqi2.Checked ? 1 : 0) + "#" +
                (checkchulicuoshichangqi3.Checked ? 1 : 0) + "#" +
                (checkchulicuoshichangqi4.Checked ? 1 : 0);

            spaf.ChangQiTengTongHuanZhe.DuiHuanZheYingXiang =
                (checkduihuanzheyingxiang1.Checked ? 1 : 0) + "#" +
                (checkduihuanzheyingxiang2.Checked ? 1 : 0) + "#" +
                (checkduihuanzheyingxiang3.Checked ? 1 : 0) + "#" +
                (checkduihuanzheyingxiang4.Checked ? 1 : 0);

            spaf.ChangQiTengTongHuanZhe.HuanZheDuiZhiTongYaoChengYinKenNegXing =
                (checkhuanzheduizhitongyao1.Checked ? 1 : 0) + "#" +
                (checkhuanzheduizhitongyao2.Checked ? 1 : 0) + "#" +
                (checkhuanzheduizhitongyao3.Checked ? 1 : 0);

            spaf.ChangQiTengTongHuanZhe.YiZhouNeiCenYongYao =
                (checkyizhounei1.Checked ? 1 : 0) + "#" +
                (checkyizhounei2.Checked ? 1 : 0) + "#" +
                (checkyizhounei3.Checked ? 1 : 0) + "#" +
                (checkyizhounei4.Checked ? 1 : 0) + "#" +
                (checkyizhounei5.Checked ? 1 : 0);

            spaf.ChangQiTengTongHuanZhe.YongYaoHouXiaoGuo =
                (checkyongyaoxiaoguo1.Checked ? 1 : 0) + "#" +
                (checkyongyaoxiaoguo2.Checked ? 1 : 0) + "#" +
                (checkyongyaoxiaoguo3.Checked ? 1 : 0);

            spaf.ChangQiTengTongHuanZhe.YuanYin =
                (checkyuanyin1.Checked ? 1 : 0) + "#" +
                (checkyuanyin2.Checked ? 1 : 0) + "#" +
                (checkyuanyin3.Checked ? 1 : 0) + "#" +
                (checkyuanyin4.Checked ? 1 : 0) + "#" +
                (checkyuanyin5.Checked ? 1 : 0) + "#" +
                (checkyuanyin6.Checked ? 1 : 0) + "#" +
                (checkyuanyin7.Checked ? 1 : 0) + "#" +
                (checkyuanyin8.Checked ? 1 : 0);

            spaf.ZiShaQingXiangHuanZhe.ChuLioCuoShi_ZiSha =
                (checkchulicuoshizisha1.Checked ? 1 : 0) + "#" +
                (checkchulicuoshizisha2.Checked ? 1 : 0) + "#" +
                (checkchulicuoshizisha3.Checked ? 1 : 0) + "#" +
                (checkchulicuoshizisha4.Checked ? 1 : 0) + "#" +
                (checkchulicuoshizisha5.Checked ? 1 : 0);

            spaf.ZiShaQingXiangHuanZhe.WeiXianYinSu =
                (checkweixdianyinsu1.Checked ? 1 : 0) + "#" +
                (checkweixdianyinsu2.Checked ? 1 : 0) + "#" +
                (checkweixdianyinsu3.Checked ? 1 : 0) + "#" +
                (checkweixdianyinsu4.Checked ? 1 : 0) + "#" +
                (checkweixdianyinsu5.Checked ? 1 : 0) + "#" +
                (checkweixdianyinsu6.Checked ? 1 : 0);

            spaf.ZiShaQingXiangHuanZhe.ZiShaQingXiang =
                (checkzisha1.Checked ? 1 : 0) + "#" +
                (checkzisha2.Checked ? 1 : 0);

            spaf.LinZhongHuanZhe.ChuLioCuo_LinZhong =
                (checkchulicuoshilinzhong1.Checked ? 1 : 0) + "#" +
                (checkchulicuoshilinzhong2.Checked ? 1 : 0) + "#" +
                (checkchulicuoshilinzhong3.Checked ? 1 : 0) + "#" +
                (checkchulicuoshilinzhong4.Checked ? 1 : 0);

            spaf.LinZhongHuanZhe.HuanZheJiShuLinZhongQiangJiuTaiDu =
                (checkqiangjiu1.Checked ? 1 : 0) + "#" +
                (checkqiangjiu2.Checked ? 1 : 0) + "#" +
                (checkqiangjiu3.Checked ? 1 : 0);

            spaf.LinZhongHuanZhe.HuanZheQingXu =
                (checkhuanzheqingxu1.Checked ? 1 : 0) + "#" +
                (checkhuanzheqingxu2.Checked ? 1 : 0) + "#" +
                (checkhuanzheqingxu3.Checked ? 1 : 0) + "#" +
                (checkhuanzheqingxu4.Checked ? 1 : 0) + "#" +
                (checkhuanzheqingxu5.Checked ? 1 : 0);

            spaf.LinZhongHuanZhe.HuanZheXuQiu =
                (checkhuanzhexuqiu1.Checked ? 1 : 0) + "#" +
                (checkhuanzhexuqiu2.Checked ? 1 : 0) + "#" +
                (checkhuanzhexuqiu3.Checked ? 1 : 0);

            spaf.LinZhongHuanZhe.JiaRenXuQiu = (checkjiarenxuqiu.Checked ? 1 : 0).ToString();

            spaf.LinZhongHuanZhe.JiBingZhenDuan = textjibingzhenduan.Text;

            spaf.LinZhongHuanZhe.LZHZ =
                (checklinzhonghuanzhe1.Checked ? 1 : 0) + "#" +
                (checklinzhonghuanzhe2.Checked ? 1 : 0);

            spaf.LinZhongHuanZhe.ZhuYaoZhengZhuang =
                (checkzhuyaozhengzhuang1.Checked ? 1 : 0) + "#" +
                (checkzhuyaozhengzhuang2.Checked ? 1 : 0) + "#" +
                (checkzhuyaozhengzhuang3.Checked ? 1 : 0) + "#" +
                (checkzhuyaozhengzhuang4.Checked ? 1 : 0) + "#" +
                (checkzhuyaozhengzhuang5.Checked ? 1 : 0) + "#" +
                (checkzhuyaozhengzhuang6.Checked ? 1 : 0) + "#" +
                (checkzhuyaozhengzhuang7.Checked ? 1 : 0) + "#" +
                (checkzhuyaozhengzhuang8.Checked ? 1 : 0);

            spaf.JinShenZhangAiHuanZhe.ChuLioCuo_JingShen =
                (checkchulicuoshijingshen1.Checked ? 1 : 0) + "#" +
                (checkchulicuoshijingshen2.Checked ? 1 : 0) + "#" +
                (checkchulicuoshijingshen3.Checked ? 1 : 0) + "#" +
                (checkchulicuoshijingshen4.Checked ? 1 : 0);

            spaf.JinShenZhangAiHuanZhe.FuFaXianXiang =
                (checkfuzaxiangxiang1.Checked ? 1 : 0) + "#" +
                (checkfuzaxiangxiang2.Checked ? 1 : 0);

            spaf.JinShenZhangAiHuanZhe.JiaZuShi =
                (checkjiazushi1.Checked ? 1 : 0) + "#" +
                (checkjiazushi2.Checked ? 1 : 0);

            spaf.JinShenZhangAiHuanZhe.JingShenZhangAi =
                (checkjingshenzhangai1.Checked ? 1 : 0) + "#" +
                (checkjingshenzhangai2.Checked ? 1 : 0);

            spaf.JinShenZhangAiHuanZhe.MoCiFaBingShiJian = timemocifabingshijian.Text;

            spaf.JinShenZhangAiHuanZhe.PiHeZhiLiao =
                (checkpeihezhiliao1.Checked ? 1 : 0) + "#" +
                (checkpeihezhiliao2.Checked ? 1 : 0);

            spaf.JinShenZhangAiHuanZhe.ShouCiFaBingShiJian = timeshoucifabingshijian.Text;

            spaf.JinShenZhangAiHuanZhe.XinLiZhuangKuang =
                (checkxinlizhuangkuang1.Checked ? 1 : 0) + "#" +
                (checkxinlizhuangkuang2.Checked ? 1 : 0) + "#" +
                (checkxinlizhuangkuang3.Checked ? 1 : 0) + "#" +
                (checkxinlizhuangkuang4.Checked ? 1 : 0);

            spaf.JinShenZhangAiHuanZhe.ZhengGuiZhiLiao =
                (checkzhengguizhiliao1.Checked ? 1 : 0) + "#" +
                (checkzhengguizhiliao2.Checked ? 1 : 0);

            spaf.JinShenZhangAiHuanZhe.XiaoGuo = textxiaoguo.Text;

            spaf.XuJiuHuanZhe.ChuLioCuo_XuJiu =
                (checkchulicuoshixiujiu1.Checked ? 1 : 0) + "#" +
                (checkchulicuoshixiujiu2.Checked ? 1 : 0) + "#" +
                (checkchulicuoshixiujiu3.Checked ? 1 : 0);

            spaf.XuJiuHuanZhe.XinLiYaLi_XuJiu =
                (checkxinliyali1.Checked ? 1 : 0) + "#" +
                (checkxinliyali2.Checked ? 1 : 0) + "#" +
                (checkxinliyali3.Checked ? 1 : 0) + "#" +
                (checkxinliyali4.Checked ? 1 : 0);

            spaf.XuJiuHuanZhe.XuJiu =
                (checkxujiu1.Checked ? 1 : 0) + "#" +
                (checkxujiu2.Checked ? 1 : 0);

            spaf.XuJiuHuanZhe.XuJiuQingKuang =
                (checkxujiuqingkuang1.Checked ? 1 : 0) + "#" +
                (checkxujiuqingkuang2.Checked ? 1 : 0) + "#" +
                (checkxujiuqingkuang3.Checked ? 1 : 0) + "#" +
                (checkxujiuqingkuang4.Checked ? 1 : 0);

            spaf.XuJiuHuanZhe.XuJiuYunaYin =
                (checkxujiuyuanyin1.Checked ? 1 : 0) + "#" +
                (checkxujiuyuanyin2.Checked ? 1 : 0) + "#" +
                (checkxujiuyuanyin3.Checked ? 1 : 0);

            spaf.XuJiuHuanZhe.XuJiuZhongDuXingJiBing =
                (checkjiujingzhongdu1.Checked ? 1 : 0) + "#" +
                (checkjiujingzhongdu2.Checked ? 1 : 0);


            //吸毒开始
            spaf.XiDuHuanZhe.ChuLioCuo_XiDu =
                (checkchulicuoshixidu1.Checked ? 1 : 0) + "#" +
                (checkchulicuoshixidu2.Checked ? 1 : 0) + "#" +
                (checkchulicuoshixidu3.Checked ? 1 : 0);

            spaf.XiDuHuanZhe.FuXiXianXiang =
                (checkfuxixianxiang1.Checked ? 1 : 0) + "#" +
                (checkfuxixianxiang2.Checked ? 1 : 0) + "#" +
                (checkfuxixianxiang3.Checked ? 1 : 0);

            spaf.XiDuHuanZhe.HBsAg =
                (checkhbsag1.Checked ? 1 : 0) + "#" +
                (checkhbsag2.Checked ? 1 : 0) + "#" +
                (checkhbsag3.Checked ? 1 : 0);

            spaf.XiDuHuanZhe.KangHIV =
                (checkkanghiv1.Checked ? 1 : 0) + "#" +
                (checkkanghiv2.Checked ? 1 : 0) + "#" +
                (checkkanghiv3.Checked ? 1 : 0);

            spaf.XiDuHuanZhe.XDHZ =
                (checkxiduhuanzhe1.Checked ? 1 : 0) + "#" +
                (checkxiduhuanzhe2.Checked ? 1 : 0);

            spaf.XiDuHuanZhe.XiDuFangShi =
                (checkxidufangshi1.Checked ? 1 : 0) + "#" +
                (checkxidufangshi2.Checked ? 1 : 0) + "#" +
                (checkxidufangshi3.Checked ? 1 : 0);

            spaf.XiDuHuanZhe.XiDuPinLv =
                (checkxidupinlv1.Checked ? 1 : 0) + "#" +
                (checkxidupinlv2.Checked ? 1 : 0);

            spaf.XiDuHuanZhe.XinLiYaLi_XiDu =
                (checkxinliyalixidu1.Checked ? 1 : 0) + "#" +
                (checkxinliyalixidu2.Checked ? 1 : 0) + "#" +
                (checkxinliyalixidu3.Checked ? 1 : 0) + "#" +
                (checkxinliyalixidu4.Checked ? 1 : 0);

            spaf.XiDuHuanZhe.ZhengGuiJieDuZhiLiao =
                (checkzhengguijiedu1.Checked ? 1 : 0) + "#" +
                 (checkzhengguijiedu2.Checked ? 1 : 0);

            //签名开始 护士签名字段 如果 是新增的并且输入框为空的话 使用系统当前护士签名

            spaf.HuShiQianMing = (string.IsNullOrEmpty(this.Id) && string.IsNullOrEmpty(textqianming.Text)) ? LoginUser.UserName : textqianming.Text;
            spaf.RiQi = dateEditriqi.Text;

            spaf.FormId = (int)FormType.NewSpaForm;
            spaf.FormName = EnumHelper.GetDescription(FormType.NewSpaForm);
            record.NursingFormData = XmlUtil.ObjectToXML(spaf, Encoding.UTF8);
            //表单ID
            record.FormId = (int)FormType.NewSpaForm;
            record.FormName = EnumHelper.GetDescription(FormType.NewSpaForm);
            //住院号
            record.PatientId = this.PatientId;
            //住院次数
            record.VisitId = 0;
            //患者姓名
            record.PatientName = this.PatientName;
            //床号
            record.BedNo = "";
            //当前患者科室ID
            record.DepartmentId = "";
            //当前患者科室名称
            record.DepartmentName = "";
            //当前患者病区ID
            record.AreaId = "";
            //当前患者病区名称
            record.AreaName = "";
            record.AssessmentDate = dateEditriqi.DateTime;
            record.Id = Id == "" ? 0 : Convert.ToInt32(Id);


            //判断患者ID唯一键 没有则添加,有则修改
            string sqlStr = @"if(EXISTS (SELECT TOP 1 Id FROM NursingFormRecord(NOLOCK) WHERE FormId = '{8}' AND PatientId='{0}' ))
                            BEGIN UPDATE NursingFormRecord SET NursingFormData='{10}'  WHERE FormId = '{8}' AND PatientId='{0}' END ELSE BEGIN
                           INSERT INTO [NursingFormRecord] ([PatientId],[VisitId],[PatientName] ,[BedNo] ,[DepartmentId] ,[DepartmentName] ,[AreaId],[AreaName] ,[FormId] ,[FormName] ,[NursingFormData] ,[AssessmentDate],[CreateTime])
                            VALUES('{0}','{1}','{2}' ,'{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}') END";
            sqlStr = string.Format(sqlStr, record.PatientId, record.VisitId, record.PatientName, record.BedNo, record.DepartmentId, record.DepartmentName, record.AreaId, record.AreaName,
                record.FormId, record.FormName, record.NursingFormData, record.AssessmentDate, DateTime.Now);
            int count = SqlHelper.ExecuteNonQuery(sqlStr);
            if (count > 0)
            {
                MessageBox.Show("保存成功");
                BindInfo(this.PatientId);
            }
        }
        #endregion

        #region 打印
        private void btnPrint_Click(object sender, EventArgs e)
        {
            string sqlStr = " SELECT TOP 1 Id FROM dbo.NursingFormRecord(NOLOCK) WHERE PatientId='{0}' AND FormId='{1}' ORDER BY  AssessmentDate DESC ";
            sqlStr = string.Format(sqlStr, PatientId, (int)FormType.NewSpaForm);
            object id = SqlHelper.ExecuteScalar(sqlStr);
            if (id == DBNull.Value) return;
            if (id == null) return;
            if (string.IsNullOrEmpty(id.ToString())) return;
            this.Id = id.ToString();
            if (string.IsNullOrEmpty(id.ToString()))
            {
                MessageBox.Show("当前为新增模式，无法操作");
                return;
            }
            string sql = string.Format("SELECT TOP 1 AssessmentDate FROM NursingFormRecord(NOLOCK) WHERE PatientId='{0}' AND FormId='{1}'", this.PatientId, (int)FormType.NewSpaForm);
            object assessmentDate = SqlHelper.ExecuteScalar(CommandType.Text, sql);
            if (assessmentDate == DBNull.Value) return;
            if (assessmentDate == null) return;
            if (string.IsNullOrEmpty(assessmentDate.ToString())) return;
            DateTime dateTime = Convert.ToDateTime(assessmentDate);
            PrintSPAF pss = new PrintSPAF(LoginUser, this.PatientId, this.PatientName, dateTime, dateTime, this.Id);
            pss.Show();
        }
        #endregion

    }
}
